package com.yuwubao.hz_International.services.impl;

import com.yuwubao.hz_International.entities.ConsumerEntity;
import com.yuwubao.hz_International.entities.repository.ConsumerRepository;
import com.yuwubao.hz_International.entities.vo.ConsumerVo;
import com.yuwubao.hz_International.services.ConsumerService;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yangyu on 2018/1/25.
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerRepository consumerRepository;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public ConsumerEntity findByPhone(String phone) {
        return consumerRepository.findByPhone(phone);
    }

    @Override
    public ConsumerEntity save(ConsumerEntity consumerEntity) {
        return consumerRepository.save(consumerEntity);
    }

    @Override
    public ConsumerEntity findByPhoneAndPassword(String phone, String pwd) {
        return consumerRepository.findByPhoneAndPassword(phone, pwd);
    }

    @Override
    public List<ConsumerVo> findConsumerList(String consumerName, int index, int size) {
        String sql = "SELECT id, name, isStore, createTime, money, welfareMoney, bonous, giftMoney FROM consumer WHERE 1 ";
        if (StringUtils.isNotBlank(consumerName)) {
            sql += " AND name LIKE '%" + consumerName + "%'";
        }
        if (size != 0) {
            if (index == 0) {
                sql += " LIMIT " + index + "," + size;
            } else {
                sql += " LIMIT " + index*size + "," + size;
            }
        }
        RowMapper<ConsumerVo> rowMapper = new BeanPropertyRowMapper<ConsumerVo>(ConsumerVo.class);
        List<ConsumerVo> list = jdbcTemplate.query(sql, rowMapper);
        return list;
    }

    @Override
    public ConsumerEntity findOne(int consumerId) {
        return consumerRepository.findOne(consumerId);
    }

    @Override
    public ConsumerEntity update(ConsumerEntity consumerEntity) {
        ConsumerEntity oldEntity = consumerRepository.findOne(consumerEntity.getId());
        if (oldEntity != null) {
            ModelMapper model = new ModelMapper();
            ConsumerEntity entity = model.map(consumerEntity, ConsumerEntity.class);
            return consumerRepository.save(entity);
        }
        return null;
    }

    @Override
    public ConsumerEntity findByName(String consumerName) {
        return consumerRepository.findByName(consumerName);
    }

    @Override
    public ConsumerVo findById(int consumerId) {
        String sql = "SELECT id, name, isStore, createTime, money, welfareMoney, bonous, giftMoney FROM consumer WHERE id = ?";
        RowMapper<ConsumerVo> rowMapper = new BeanPropertyRowMapper<ConsumerVo>(ConsumerVo.class);
        ConsumerVo consumerVo = jdbcTemplate.queryForObject(sql, rowMapper, consumerId);
        return consumerVo;
    }

    @Override
    public ConsumerEntity delete(int consumerId) {
        ConsumerEntity consumerEntity = consumerRepository.findOne(consumerId);
        if (consumerEntity != null) {
            consumerRepository.delete(consumerId);
            return consumerEntity;
        }
        return null;
    }
}
